package com.heima.controller;


import cn.hutool.core.bean.BeanUtil;
import com.heima.pojo.TbUser;
import com.heima.pojo.dto.UserDto;
import com.heima.pojo.vo.PageResponse;
import com.heima.pojo.vo.UserVo;
import com.heima.service.ITbUserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author nan
 * @since 2024-06-11
 */
@RestController
@RequestMapping("/api/user-service/user")
public class TbUserController {

    @Autowired
    private ITbUserService userService;

    @GetMapping("/search")
    @ApiOperation("人员搜索")
    public PageResponse<UserVo> getByPage(@RequestParam Integer pageIndex,
                                          @RequestParam Integer pageSize,
                                          @RequestParam(required = false) String userName) {

        return userService.getByPage(pageIndex, pageSize, userName);
    }

    @PostMapping
    @ApiOperation("新增人员")
    public Boolean addUser(@RequestBody UserDto userDto) {
        TbUser user = BeanUtil.toBean(userDto, TbUser.class);
        if (userDto.getStatus()) {
            user.setStatus(1);
        } else {
            user.setStatus(0);
        }
        return userService.save(user);
    }

    @PutMapping("/{userId}")
    @ApiOperation("修改人员")
    public Boolean updateUser(@PathVariable Integer userId, @RequestBody UserDto userDto) {
        TbUser user = BeanUtil.toBean(userDto, TbUser.class);
        user.setId(userId);
        if (userDto.getStatus()) {
            user.setStatus(1);
        } else {
            user.setStatus(0);
        }
        return userService.updateById(user);
    }

    @DeleteMapping("/{userId}")
    @ApiOperation("删除人员")
    public Boolean deleteUserById(@PathVariable Integer userId) {
        return userService.removeById(userId);
    }

}
